package com.ajzhang.action;

import java.io.PrintWriter;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.httpclient.util.DateParseException;
import org.apache.commons.httpclient.util.DateUtil;
import org.apache.struts2.ServletActionContext;

import com.ajzhang.bean.Bill;
import com.ajzhang.bean.BillType;
import com.ajzhang.bean.UserInfo;
import com.ajzhang.facade.BillFacade;
import com.ajzhang.facade.BillTypeFacade;
import com.opensymphony.xwork2.ActionSupport;

public class BillAction extends ActionSupport {

	private Bill bill;
	private BillTypeFacade billTypeFacade;
	private BillFacade billFacade;

	@Override
	public String execute() throws Exception {
		return super.execute();
	}

	public String save() throws Exception {
		System.out.println("into BillAction -> findByDateTime()");
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		PrintWriter out = response.getWriter();
		try {
			double money = Double.parseDouble(request.getParameter("money"));
			String remark = request.getParameter("remark");
			String billTypeName = request.getParameter("billTypeName");
			List<BillType> billTypeList = this.billTypeFacade.findAll();
			BillType billType = new BillType();
			for (BillType type : billTypeList) {
				if(type.getName().equals(billTypeName)){
					billType = type;
					break;
				}
			}
			bill = new Bill();
			UserInfo userInfo = (UserInfo) request.getSession().getAttribute("UserInfo");
			bill.setBillTypeId(billType);
			bill.setCreateDate(new Date());
			bill.setDate(new Date());
			bill.setMoney(money);
			bill.setRemark(remark);
			bill.setUserId(userInfo);
			this.billFacade.save(bill);
			out.print("success");
		} catch (Exception e) {
			System.out.println(e.getMessage()+":保存记录时报错。");
			out.print("error");
		}
		return null;
	}

	public String delete() throws Exception {
		return null;
	}

	public String query() throws Exception {
		return null;
	}

	public String findByDateTime() throws Exception {
		System.out.println("into BillAction -> findByDateTime()");
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		PrintWriter out = response.getWriter();
		JSONArray json = new JSONArray();
		try {
			Date endTime = com.ajzhang.util.DateUtil.toDate(request.getParameter("DateTime"),com.ajzhang.util.DateUtil.YEAR_MONTH_DATE_FORMAT);
			UserInfo userInfo = (UserInfo) request.getSession().getAttribute("UserInfo");
			Calendar c = Calendar.getInstance();
			for (int i = -5; i < 1; i++) {
				JSONObject jsonObj = new JSONObject();
				c.setTime(endTime);
				c.add(Calendar.DATE, i);
				String dateTime = com.ajzhang.util.DateUtil.toString(c.getTime(),"yyyy-MM-dd");
				double money = this.billFacade.findPageTodayByCriteria(dateTime, userInfo);
				jsonObj.put("dateTime",dateTime);
				jsonObj.put("money", money);
				jsonObj.put("day", c.get(Calendar.DAY_OF_MONTH));
				jsonObj.put("month", c.get(Calendar.MONTH)+1);
				jsonObj.put("year", c.get(Calendar.YEAR));
				json.add(jsonObj);
			}
			out.print(json);
		} catch (Exception e) {
			System.out.println(e.getMessage());
			out.print("");
		}
		return null;
	}
	
	public String findTodaybyDateTime() throws Exception {
		System.out.println("into BillAction -> findTodaybyDateTime()");
		HttpServletRequest request = ServletActionContext.getRequest();
		Date date = com.ajzhang.util.DateUtil.toDate(request.getParameter("DateTime"),com.ajzhang.util.DateUtil.YEAR_MONTH_DATE_FORMAT);
		Date endTime = com.ajzhang.util.DateUtil.toDate(request.getParameter("DateTime")+" 23:59:59");
		Integer pageIndex = 0;
		try {
			pageIndex = Integer.parseInt(request.getParameter("pageIndex"));
		} catch (Exception e) {
			pageIndex = 0;
		}
		UserInfo userInfo = (UserInfo) request.getSession().getAttribute("UserInfo");
		Integer count = this.billFacade.findTodayCountByCriteria(date, endTime, userInfo);
		request.setAttribute("pageIndex", pageIndex);
		request.setAttribute("count", (int)Math.ceil((double)count / 10));
		request.setAttribute("BillList", this.billFacade.findPageTodayByCriteria(pageIndex,date, endTime,userInfo));
		return "list";
	}

	public static void main(String[] args) {
//		Date endTime = com.ajzhang.util.DateUtil.toDate("2013-09-01",com.ajzhang.util.DateUtil.YEAR_MONTH_DATE_FORMAT);
//		Calendar c = Calendar.getInstance();
//		c.setTime(endTime);
//		c.add(Calendar.DATE, -5);
//		System.out.println(com.ajzhang.util.DateUtil.toString(c.getTime(),"yyyy-MM-dd"));
//		c.setTime(endTime);
//		c.add(Calendar.DATE, -4);
//		System.out.println(com.ajzhang.util.DateUtil.toString(c.getTime(),"yyyy-MM-dd"));
//		c.setTime(endTime);
//		c.add(Calendar.DATE, -3);
//		System.out.println(com.ajzhang.util.DateUtil.toString(c.getTime(),"yyyy-MM-dd"));
//		c.setTime(endTime);
//		c.add(Calendar.DATE, -2);
//		System.out.println(com.ajzhang.util.DateUtil.toString(c.getTime(),"yyyy-MM-dd"));
//		c.setTime(endTime);
//		c.add(Calendar.DATE, -1);
//		System.out.println(com.ajzhang.util.DateUtil.toString(c.getTime(),"yyyy-MM-dd"));
//		c.setTime(endTime);
//		c.add(Calendar.DATE, 0);
//		System.out.println(com.ajzhang.util.DateUtil.toString(c.getTime(),"yyyy-MM-dd"));
	}

	public Bill getBill() {
		return bill;
	}

	public void setBill(Bill bill) {
		this.bill = bill;
	}

	public BillFacade getBillFacade() {
		return billFacade;
	}

	public void setBillFacade(BillFacade billFacade) {
		this.billFacade = billFacade;
	}

	public BillTypeFacade getBillTypeFacade() {
		return billTypeFacade;
	}

	public void setBillTypeFacade(BillTypeFacade billTypeFacade) {
		this.billTypeFacade = billTypeFacade;
	}
	
}
